package j.framework.tags.easyui;

import j.framework.tags.form.FormTagWriter;
import j.util.StringUtils;

import javax.servlet.jsp.JspException;
import java.util.Map;

/**
 * 单选文本输入框或多行文本输入框
 * 多行文本输入框必须指定multiline为true并且同时指定高度值
 * User: jason
 * Date: 2015/1/16 17:21
 */
public class TextBoxTag extends BaseEasyuiFormControl {
    private Integer width;
    private Integer height;
    private String value;
    private String type;//text or password,默认为text
    private boolean multiline; //是否允许多行
    private Boolean editable;//是否允许编辑内容
    private String buttonText;
    private String onClickButton;
    private String onEnterPressed;//当在此文本框输入后按下回车键时所执行的事件

    private String onChange;

    @Override
    public void writeFormTagBegin(FormTagWriter writer) throws JspException {
        writer.beginTag("input");
    }



    @Override
    public void writeFormTagProperties(FormTagWriter writer) throws JspException {
        if(StringUtils.isNullOrEmpty(type)){
            writer.writeFormTagProperty("type","text");
        }else{
            writer.writeFormTagProperty("type",type);
        }
        if(StringUtils.isNotEmpty(value)){
            writer.writeFormTagProperty("value",value);
        }else{
            Object obj = getFormDataValue();
            if(obj != null){
                writer.writeFormTagProperty("value",obj.toString());
            }
        }
    }

    @Override
    protected String getControlWidth() {
        if(width==null || width<=0){
            return "100%";
        }
        return width.toString()+"px";
    }

    @Override
    public void writeEasyuiDataOptions(Map<String, Object> dataOptions) throws JspException {
        if(width!=null && width>0){
            dataOptions.put("width",width);
        }
        if(height!=null && height>0){
            dataOptions.put("height",height);
        }
        if(multiline){
            dataOptions.put("multiline",true);
        }
        if(editable!=null && !editable){
            dataOptions.put("editable",false);
        }
        if(StringUtils.isNotEmpty(buttonText)){
            dataOptions.put("buttonText",buttonText);
        }
        if(StringUtils.isNotEmpty(onClickButton)){
            dataOptions.put("onClickButton",onClickButton);
        }
        if(StringUtils.isNotEmpty(onChange)){
            dataOptions.put("onChange",onChange);
        }
    }

    @Override
    public void endBeginTag(FormTagWriter writer) throws JspException {
        writer.closeBlankTag();
        if(StringUtils.isNotEmpty(onEnterPressed)){
            StringBuilder s = new StringBuilder("<script>$('#").append(name).append("').textbox('textbox').bind('keydown',function(e){");
            //构造回车事件
            s.append("alert(e);if(e.keyCode == 13){");
            s.append("alert($(this).val());");
            s.append("}");
            s.append("});</script>");
            writer.writeSomething(s.toString());
        }
    }

    @Override
    public String getEasyuiClassName() {
        return "easyui-textbox";
    }

    public void setWidth(Integer width) {
        this.width = width;
    }

    public void setHeight(Integer height) {
        this.height = height;
    }

    public void setValue(String value) {
        this.value = value;
    }

    public void setType(String type) {
        this.type = type;
    }

    public void setMultiline(boolean multiline) {
        this.multiline = multiline;
    }

    public void setEditable(Boolean editable) {
        this.editable = editable;
    }

    public void setButtonText(String buttonText) {
        this.buttonText = buttonText;
    }

    public void setOnClickButton(String onClickButton) {
        this.onClickButton = onClickButton;
    }

    public void setOnEnterPressed(String onEnterPressed) {
        this.onEnterPressed = onEnterPressed;
    }

    public void setOnChange(String onChange) {
        this.onChange = onChange;
    }
}
